package org.boyan.cases;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.boyan.entity.cases.DemandInfo;
import org.boyan.entity.cases.NonFunctionalDemand;
import org.boyan.utils.ExcelUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

public class ExcelToNonFunctionalDemand {

    //将excel文件转换为肺功能需求类
    public static NonFunctionalDemand getNonFunctionalDemand(Object fileObject){
        Workbook workbook=null;
        if(fileObject instanceof MultipartFile){
            MultipartFile file= (MultipartFile) fileObject;
            workbook= ExcelUtils.getWorkBook(file);
        }else if(fileObject instanceof File){
            File file= (File) fileObject;
            workbook=ExcelUtils.getWorkBook(file);
        }
        NonFunctionalDemand nonFunctionalDemand=new NonFunctionalDemand();
        try{
            Sheet sheet=workbook.getSheetAt(2);
            nonFunctionalDemand.setTransServerTime(sheet.getRow(0).getCell(1).getStringCellValue());
            //此处排除excel文件中关于单元格内填写的是数组的问题
            CellType concurrencyUserAmountCellType=sheet.getRow(0).getCell(3).getCellType();
            String concurrencyUserAmount;
            if(concurrencyUserAmountCellType==CellType.NUMERIC){
                concurrencyUserAmount=String.valueOf(sheet.getRow(0).getCell(3).getNumericCellValue());
            }else{
                concurrencyUserAmount=sheet.getRow(0).getCell(3).getStringCellValue();
            }
            nonFunctionalDemand.setConcurrencyUserAmount(concurrencyUserAmount);
            nonFunctionalDemand.setResponseTime(sheet.getRow(1).getCell(1).getStringCellValue());
            nonFunctionalDemand.setTpsMaximum(sheet.getRow(1).getCell(3).getStringCellValue());
            nonFunctionalDemand.setDateQueryRange(sheet.getRow(2).getCell(1).getStringCellValue());
            nonFunctionalDemand.setSingleQueryRange(sheet.getRow(2).getCell(3).getStringCellValue());
            nonFunctionalDemand.setPaginationViewAmount(sheet.getRow(3).getCell(1).getStringCellValue());
            nonFunctionalDemand.setDailyTransAmount(sheet.getRow(3).getCell(3).getStringCellValue());
            String printFlag=sheet.getRow(4).getCell(1).getStringCellValue();
            if("是".equals(printFlag)||"Y".equals(printFlag)){
                nonFunctionalDemand.setPrintFlag(1);
                nonFunctionalDemand.setCreateFileRange(sheet.getRow(4).getCell(3).getStringCellValue());
            }else{
                nonFunctionalDemand.setPrintFlag(0);
            }
            String calculateFlag=sheet.getRow(5).getCell(1).getStringCellValue();
            if("是".equals(calculateFlag)||"Y".equals(calculateFlag)){
                nonFunctionalDemand.setCalculateFlag(1);
                nonFunctionalDemand.setCalculateDescription(sheet.getRow(5).getCell(3).getStringCellValue());
            }else{
                nonFunctionalDemand.setCalculateFlag(0);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return nonFunctionalDemand;
    }

}
